import {observable,action} from 'mobx';
import * as DoorPageAPI from '../../apis/task/doorPage'

export default class Doorpage {
    @observable count = {todaytask:0,flowtask:0};
    @observable loading =  true;
    @observable datakey = 0;
    @observable binputstyle = {width: 100,display:'none'};
    @observable inputval = "";
    @observable dataSource = [];
    @observable currentindex = 1;
    @observable pageNum = 1;
    @observable totalPage = 1;
    @observable addId = "";
    @observable addtaskval = "";
    @observable status = {createvisable:false};
    constructor() {

    }
    //获取数据
    @action
    getDatas = params => {
        DoorPageAPI.getDoorTaskList(params).then(action((data) => {
            this.dataSource = data.datas;
            this.totalPage = data.totalpage;
            this.loading = false;
            if(params.firstreq!=undefined && params.firstreq==1 ){
                this.count.todaytask = data.todaycount;
                this.count.flowtask = data.nextcount;
            }else if(data.indexid==1){
                this.count.todaytask = data.todaycount;
            }else if(data.indexid==2){
                this.count.flowtask = data.nextcount;
            }
            this.addId = "";
        }));
    }
    //滚动加载数据
    getScrollDatas = params => {
        DoorPageAPI.getDoorTaskList(params).then(action((data) => {
            let datas = data.datas;
            this.totalPage = data.totalpage;
            for(let i=0;i<datas.length;i++){
                this.dataSource.push(datas[i]);
            }
            this.loading = false;
            //console.log(data.datas);
        }));
    }
    //修改任务状态
    toChangeStatus = params => {
        DoorPageAPI.toChangeStatus(params).then(action((data) => {
            if(data.status==1){
                this.count = {todaytask:parseInt(this.count.todaytask)-1,flowtask:this.count.flowtask};
                this.dataSource.remove(this.dataSource[params.idx]);
            }
        }));
    }
    //添加或取消关注
    setSpecial = params => {
        DoorPageAPI.setSpecial(params).then(action((data) => {
            if(data.status==1){
                if(params.special==0){
                    this.dataSource[params.idx].opttype = "取消关注";
                    this.dataSource[params.idx].changetype = 1;
                }else{
                    this.dataSource[params.idx].opttype = "添加关注";
                    this.dataSource[params.idx].changetype = 0;
                }
            }
        }));
    }
    //快速新增任务
    fastAddTask = params => {
        DoorPageAPI.fastAddTask(params).then(action((data) => {
            if(data.status==1){
                this.dataSource.unshift({
                    id:data.taskId,
                    name:params.taskName,
                    lastname:data.lastname,
                    changetype:2,
                    opttype:"标记完成"
                });
                this.count = {todaytask:parseInt(this.count.todaytask)+1,flowtask:this.count.flowtask};
                this.addId = this.addId+","+data.taskId;
                this.addtaskval = "";
            }
        }));
    }
    //dialog只关闭
    onlyClose = () => {
        this.status = {createvisable:false};
    }
    //dialog关闭并且刷新
    doClose = () => {
        this.getDatas({"indexid":this.currentindex,"pagenum":1});
        this.status = {createvisable:false};
    }
    //刷新
    doRefresh = () => {
        this.getDatas({"indexid":this.currentindex,"pagenum":1});
    }
    //dialog开启
    open = () => {
        this.status = {createvisable:true};
    }
}